package com.coming.mySpring.org.controller;

import com.coming.mySpring.org.domain.SystemDictionaryType;
import com.coming.mySpring.org.query.SystemDictionaryTypeQuery;
import com.coming.mySpring.org.service.ISystemDictionaryTypeService;
import com.coming.mySpring.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sdt")
@Api(value = "数据字典类型详细信息",description = "数据字典相关的crud功能")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    @GetMapping("/list")
    public List<SystemDictionaryType> loadAll(){
        return systemDictionaryTypeService.loadAll();
    }

    @GetMapping("/{id}")
    public AjaxResult loadOne(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.loadOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setMsg("无数据");
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/queryList")
    @ApiOperation(value = "分页查询")
    public AjaxResult queryList(@RequestBody SystemDictionaryTypeQuery query){
        try {
            List<SystemDictionaryType> pageQuery = systemDictionaryTypeService.queryList(query);
            Long queryCount = systemDictionaryTypeService.queryCount(query);
            return AjaxResult.success().setDate(pageQuery).setTotal(queryCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 根据Id删除
     * @param id
     * @return
     */
    @Transactional
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult Del(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    /**
     * 保存   /   修改
     * @param systemDictionaryType
     */
    @PutMapping
    @ApiOperation(value = "保存与修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        /*
            一、这里的保存分为两种
                1、为添加保存
                2、为修改保存
         */
        try {
            if(systemDictionaryType.getId() !=null){
                System.out.println(systemDictionaryType.getId());
                //当id不为空的时候,为修改保存
                systemDictionaryTypeService.update(systemDictionaryType);
            }else{
                //当id为空的时候,为修改保存
                systemDictionaryTypeService.save(systemDictionaryType);
            }
                return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PatchMapping("/del")
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
}
